﻿using System;
using System.Collections;
using UnityEngine;

// Token: 0x0200015C RID: 348
[DisallowMultipleComponent]
[RequireComponent(typeof(Rigidbody))]
[AddComponentMenu("RVP/C#/Vehicle Controllers/Vehicle Parent", 0)]
public class VehicleParent : MonoBehaviour
{
	// Token: 0x0600064A RID: 1610 RVA: 0x000351A8 File Offset: 0x000333A8
	private void Start()
	{
		this.tr = base.transform;
		this.rb = base.GetComponent<Rigidbody>();
		GameObject gameObject = new GameObject(this.tr.name + "'s Normal Orientation");
		this.norm = gameObject.transform;
		this.SetCenterOfMass();
		if (this.towVehicle)
		{
			this.newTow = UnityEngine.Object.Instantiate<GameObject>(this.towVehicle, Vector3.zero, this.tr.rotation);
			this.newTow.SetActive(false);
			this.newTow.transform.position = this.tr.TransformPoint(this.newTow.GetComponent<Joint>().connectedAnchor - this.newTow.GetComponent<Joint>().anchor);
			this.newTow.GetComponent<Joint>().connectedBody = this.rb;
			this.newTow.SetActive(true);
			this.newTow.GetComponent<VehicleParent>().inputInherit = this;
		}
		if (this.sparks)
		{
			this.sparks.transform.parent = null;
		}
		if (this.wheelGroups.Length > 0)
		{
			base.StartCoroutine(this.WheelCheckLoop());
		}
	}

	// Token: 0x0600064B RID: 1611 RVA: 0x000352E8 File Offset: 0x000334E8
	private void Update()
	{
		if (this.stopUpshift)
		{
			this.upshiftPressed = false;
			this.stopUpshift = false;
		}
		if (this.stopDownShift)
		{
			this.downshiftPressed = false;
			this.stopDownShift = false;
		}
		if (this.upshiftPressed)
		{
			this.stopUpshift = true;
		}
		if (this.downshiftPressed)
		{
			this.stopDownShift = true;
		}
		if (this.inputInherit)
		{
			this.InheritInputOneShot();
		}
	}

	// Token: 0x0600064C RID: 1612 RVA: 0x00035364 File Offset: 0x00033564
	private void FixedUpdate()
	{
		if (this.inputInherit)
		{
			this.InheritInput();
		}
		if (this.wheelLoopDone && this.wheelGroups.Length > 0)
		{
			this.wheelLoopDone = false;
			base.StartCoroutine(this.WheelCheckLoop());
		}
		this.GetGroundedWheels();
		if (this.groundedWheels > 0)
		{
			this.crashing = false;
		}
		this.localVelocity = this.tr.InverseTransformDirection(this.rb.velocity - this.wheelContactsVelocity);
		this.localAngularVel = this.tr.InverseTransformDirection(this.rb.angularVelocity);
		this.velMag = this.rb.velocity.magnitude;
		this.sqrVelMag = this.rb.velocity.sqrMagnitude;
		this.forwardDir = this.tr.forward;
		this.rightDir = this.tr.right;
		this.upDir = this.tr.up;
		this.forwardDot = Vector3.Dot(this.forwardDir, GlobalControl.worldUpDir);
		this.rightDot = Vector3.Dot(this.rightDir, GlobalControl.worldUpDir);
		this.upDot = Vector3.Dot(this.upDir, GlobalControl.worldUpDir);
		this.norm.transform.position = this.tr.position;
		this.norm.transform.rotation = Quaternion.LookRotation((this.groundedWheels != 0) ? this.wheelNormalAverage : this.upDir, this.forwardDir);
		if (this.groundedWheels > 0 && !this.hover && !this.accelAxisIsBrake && this.burnoutThreshold >= 0f && this.accelInput > this.burnoutThreshold && this.brakeInput > this.burnoutThreshold)
		{
			this.burnout = Mathf.Lerp(this.burnout, (5f - Mathf.Min(5f, Mathf.Abs(this.localVelocity.z))) / 5f * Mathf.Abs(this.accelInput), Time.fixedDeltaTime * (1f - this.burnoutSmoothness) * 10f);
		}
		else if (this.burnout > 0.01f)
		{
			this.burnout = Mathf.Lerp(this.burnout, 0f, Time.fixedDeltaTime * (1f - this.burnoutSmoothness) * 10f);
		}
		else
		{
			this.burnout = 0f;
		}
		if (this.engine)
		{
			this.burnout *= this.engine.health;
		}
		if (this.brakeIsReverse && this.brakeInput > 0f && this.localVelocity.z < 1f && this.burnout == 0f)
		{
			this.reversing = true;
		}
		else if (this.localVelocity.z >= 0f || this.burnout > 0f)
		{
			this.reversing = false;
		}
	}

	// Token: 0x0600064D RID: 1613 RVA: 0x000356AB File Offset: 0x000338AB
	public void SetAccel(float f)
	{
		f = Mathf.Clamp(f, -1f, 1f);
		this.accelInput = f;
	}

	// Token: 0x0600064E RID: 1614 RVA: 0x000356C6 File Offset: 0x000338C6
	public void SetBrake(float f)
	{
		this.brakeInput = ((!this.accelAxisIsBrake) ? Mathf.Clamp(f, -1f, 1f) : (-Mathf.Clamp(this.accelInput, -1f, 0f)));
	}

	// Token: 0x0600064F RID: 1615 RVA: 0x00035704 File Offset: 0x00033904
	public void SetSteer(float f)
	{
		this.steerInput = Mathf.Clamp(f, -1f, 1f);
	}

	// Token: 0x06000650 RID: 1616 RVA: 0x0003571C File Offset: 0x0003391C
	public void SetEbrake(float f)
	{
		if ((f > 0f || this.ebrakeInput > 0f) && this.holdEbrakePark && this.velMag < 1f && this.accelInput == 0f && (this.brakeInput == 0f || !this.brakeIsReverse))
		{
			this.ebrakeInput = 1f;
		}
		else
		{
			this.ebrakeInput = Mathf.Clamp01(f);
		}
	}

	// Token: 0x06000651 RID: 1617 RVA: 0x000357A6 File Offset: 0x000339A6
	public void SetBoost(bool b)
	{
		this.boostButton = b;
	}

	// Token: 0x06000652 RID: 1618 RVA: 0x000357AF File Offset: 0x000339AF
	public void SetPitch(float f)
	{
		this.pitchInput = Mathf.Clamp(f, -1f, 1f);
	}

	// Token: 0x06000653 RID: 1619 RVA: 0x000357C7 File Offset: 0x000339C7
	public void SetYaw(float f)
	{
		this.yawInput = Mathf.Clamp(f, -1f, 1f);
	}

	// Token: 0x06000654 RID: 1620 RVA: 0x000357DF File Offset: 0x000339DF
	public void SetRoll(float f)
	{
		this.rollInput = Mathf.Clamp(f, -1f, 1f);
	}

	// Token: 0x06000655 RID: 1621 RVA: 0x000357F7 File Offset: 0x000339F7
	public void PressUpshift()
	{
		this.upshiftPressed = true;
	}

	// Token: 0x06000656 RID: 1622 RVA: 0x00035800 File Offset: 0x00033A00
	public void PressDownshift()
	{
		this.downshiftPressed = true;
	}

	// Token: 0x06000657 RID: 1623 RVA: 0x00035809 File Offset: 0x00033A09
	public void SetUpshift(float f)
	{
		this.upshiftHold = f;
	}

	// Token: 0x06000658 RID: 1624 RVA: 0x00035812 File Offset: 0x00033A12
	public void SetDownshift(float f)
	{
		this.downshiftHold = f;
	}

	// Token: 0x06000659 RID: 1625 RVA: 0x0003581C File Offset: 0x00033A1C
	private void InheritInput()
	{
		this.accelInput = this.inputInherit.accelInput;
		this.brakeInput = this.inputInherit.brakeInput;
		this.steerInput = this.inputInherit.steerInput;
		this.ebrakeInput = this.inputInherit.ebrakeInput;
		this.pitchInput = this.inputInherit.pitchInput;
		this.yawInput = this.inputInherit.yawInput;
		this.rollInput = this.inputInherit.rollInput;
	}

	// Token: 0x0600065A RID: 1626 RVA: 0x000358A0 File Offset: 0x00033AA0
	private void InheritInputOneShot()
	{
		this.upshiftPressed = this.inputInherit.upshiftPressed;
		this.downshiftPressed = this.inputInherit.downshiftPressed;
	}

	// Token: 0x0600065B RID: 1627 RVA: 0x000358C4 File Offset: 0x00033AC4
	private void SetCenterOfMass()
	{
		float num = 0f;
		if (this.suspensionCenterOfMass)
		{
			if (this.hover)
			{
				for (int i = 0; i < this.hoverWheels.Length; i++)
				{
					num = ((i != 0) ? ((num + this.hoverWheels[i].hoverDistance) * 0.5f) : this.hoverWheels[i].hoverDistance);
				}
			}
			else
			{
				for (int j = 0; j < this.wheels.Length; j++)
				{
					float suspensionDistance = this.wheels[j].transform.parent.GetComponent<Suspension>().suspensionDistance;
					num = ((j != 0) ? ((num + suspensionDistance) * 0.5f) : suspensionDistance);
				}
			}
		}
		this.rb.centerOfMass = this.centerOfMassOffset + new Vector3(0f, -num, 0f);
		this.rb.inertiaTensor = this.rb.inertiaTensor;
	}

	// Token: 0x0600065C RID: 1628 RVA: 0x000359C4 File Offset: 0x00033BC4
	private void GetGroundedWheels()
	{
		this.groundedWheels = 0;
		this.wheelContactsVelocity = Vector3.zero;
		if (this.hover)
		{
			for (int i = 0; i < this.hoverWheels.Length; i++)
			{
				if (this.hoverWheels[i].grounded)
				{
					this.wheelNormalAverage = ((i != 0) ? (this.wheelNormalAverage + this.hoverWheels[i].contactPoint.normal).normalized : this.hoverWheels[i].contactPoint.normal);
				}
				if (this.hoverWheels[i].grounded)
				{
					this.groundedWheels++;
				}
			}
		}
		else
		{
			for (int j = 0; j < this.wheels.Length; j++)
			{
				if (this.wheels[j].grounded)
				{
					this.wheelContactsVelocity = ((j != 0) ? ((this.wheelContactsVelocity + this.wheels[j].contactVelocity) * 0.5f) : this.wheels[j].contactVelocity);
					this.wheelNormalAverage = ((j != 0) ? (this.wheelNormalAverage + this.wheels[j].contactPoint.normal).normalized : this.wheels[j].contactPoint.normal);
				}
				if (this.wheels[j].grounded)
				{
					this.groundedWheels++;
				}
			}
		}
	}

	// Token: 0x0600065D RID: 1629 RVA: 0x00035B5C File Offset: 0x00033D5C
	private void OnCollisionEnter(Collision col)
	{
		if (col.contacts.Length > 0 && this.groundedWheels == 0)
		{
			foreach (ContactPoint contactPoint in col.contacts)
			{
				if (!contactPoint.thisCollider.CompareTag("Underside") && contactPoint.thisCollider.gameObject.layer != GlobalControl.ignoreWheelCastLayer && Vector3.Dot(contactPoint.normal, col.relativeVelocity.normalized) > 0.2f && col.relativeVelocity.sqrMagnitude > 20f)
				{
					bool flag = true;
					if (this.newTow)
					{
						flag = !contactPoint.otherCollider.transform.IsChildOf(this.newTow.transform);
					}
					if (flag)
					{
						this.crashing = this.canCrash;
						if (this.crashSnd && this.crashClips.Length > 0 && this.playCrashSounds)
						{
							this.crashSnd.PlayOneShot(this.crashClips[UnityEngine.Random.Range(0, this.crashClips.Length)], Mathf.Clamp01(col.relativeVelocity.magnitude * 0.1f));
						}
						if (this.sparks && this.playCrashSparks)
						{
							this.sparks.transform.position = contactPoint.point;
							this.sparks.transform.rotation = Quaternion.LookRotation(col.relativeVelocity.normalized, contactPoint.normal);
							this.sparks.Play();
						}
					}
				}
			}
		}
	}

	// Token: 0x0600065E RID: 1630 RVA: 0x00035D2C File Offset: 0x00033F2C
	private void OnCollisionStay(Collision col)
	{
		if (col.contacts.Length > 0 && this.groundedWheels == 0)
		{
			foreach (ContactPoint contactPoint in col.contacts)
			{
				if (!contactPoint.thisCollider.CompareTag("Underside") && contactPoint.thisCollider.gameObject.layer != GlobalControl.ignoreWheelCastLayer && col.relativeVelocity.sqrMagnitude < 5f)
				{
					bool flag = true;
					if (this.newTow)
					{
						flag = !contactPoint.otherCollider.transform.IsChildOf(this.newTow.transform);
					}
					if (flag)
					{
						this.crashing = this.canCrash;
					}
				}
			}
		}
	}

	// Token: 0x0600065F RID: 1631 RVA: 0x00035E0C File Offset: 0x0003400C
	private void OnDestroy()
	{
		if (this.norm)
		{
			UnityEngine.Object.Destroy(this.norm.gameObject);
		}
		if (this.sparks)
		{
			UnityEngine.Object.Destroy(this.sparks.gameObject);
		}
	}

	// Token: 0x06000660 RID: 1632 RVA: 0x00035E5C File Offset: 0x0003405C
	private IEnumerator WheelCheckLoop()
	{
		for (int i = 0; i < this.wheelGroups.Length; i++)
		{
			this.wheelGroups[i].Activate();
			this.wheelGroups[(i != 0) ? (i - 1) : (this.wheelGroups.Length - 1)].Deactivate();
			yield return new WaitForFixedUpdate();
		}
		this.wheelLoopDone = true;
		yield break;
	}

	// Token: 0x040009EC RID: 2540
	[NonSerialized]
	public Rigidbody rb;

	// Token: 0x040009ED RID: 2541
	[NonSerialized]
	public Transform tr;

	// Token: 0x040009EE RID: 2542
	[NonSerialized]
	public Transform norm;

	// Token: 0x040009EF RID: 2543
	[NonSerialized]
	public float accelInput;

	// Token: 0x040009F0 RID: 2544
	[NonSerialized]
	public float brakeInput;

	// Token: 0x040009F1 RID: 2545
	[NonSerialized]
	public float steerInput;

	// Token: 0x040009F2 RID: 2546
	[NonSerialized]
	public float ebrakeInput;

	// Token: 0x040009F3 RID: 2547
	[NonSerialized]
	public bool boostButton;

	// Token: 0x040009F4 RID: 2548
	[NonSerialized]
	public bool upshiftPressed;

	// Token: 0x040009F5 RID: 2549
	[NonSerialized]
	public bool downshiftPressed;

	// Token: 0x040009F6 RID: 2550
	[NonSerialized]
	public float upshiftHold;

	// Token: 0x040009F7 RID: 2551
	[NonSerialized]
	public float downshiftHold;

	// Token: 0x040009F8 RID: 2552
	[NonSerialized]
	public float pitchInput;

	// Token: 0x040009F9 RID: 2553
	[NonSerialized]
	public float yawInput;

	// Token: 0x040009FA RID: 2554
	[NonSerialized]
	public float rollInput;

	// Token: 0x040009FB RID: 2555
	[Tooltip("Accel axis is used for brake input")]
	public bool accelAxisIsBrake;

	// Token: 0x040009FC RID: 2556
	[Tooltip("Brake input will act as reverse input")]
	public bool brakeIsReverse;

	// Token: 0x040009FD RID: 2557
	[Tooltip("Automatically hold ebrake if it's pressed while parked")]
	public bool holdEbrakePark;

	// Token: 0x040009FE RID: 2558
	public float burnoutThreshold = 0.9f;

	// Token: 0x040009FF RID: 2559
	[NonSerialized]
	public float burnout;

	// Token: 0x04000A00 RID: 2560
	public float burnoutSpin = 5f;

	// Token: 0x04000A01 RID: 2561
	[Range(0f, 0.9f)]
	public float burnoutSmoothness = 0.5f;

	// Token: 0x04000A02 RID: 2562
	public Motor engine;

	// Token: 0x04000A03 RID: 2563
	private bool stopUpshift;

	// Token: 0x04000A04 RID: 2564
	private bool stopDownShift;

	// Token: 0x04000A05 RID: 2565
	[NonSerialized]
	public Vector3 localVelocity;

	// Token: 0x04000A06 RID: 2566
	[NonSerialized]
	public Vector3 localAngularVel;

	// Token: 0x04000A07 RID: 2567
	[NonSerialized]
	public Vector3 forwardDir;

	// Token: 0x04000A08 RID: 2568
	[NonSerialized]
	public Vector3 rightDir;

	// Token: 0x04000A09 RID: 2569
	[NonSerialized]
	public Vector3 upDir;

	// Token: 0x04000A0A RID: 2570
	[NonSerialized]
	public float forwardDot;

	// Token: 0x04000A0B RID: 2571
	[NonSerialized]
	public float rightDot;

	// Token: 0x04000A0C RID: 2572
	[NonSerialized]
	public float upDot;

	// Token: 0x04000A0D RID: 2573
	[NonSerialized]
	public float velMag;

	// Token: 0x04000A0E RID: 2574
	[NonSerialized]
	public float sqrVelMag;

	// Token: 0x04000A0F RID: 2575
	[NonSerialized]
	public bool reversing;

	// Token: 0x04000A10 RID: 2576
	public Wheel[] wheels;

	// Token: 0x04000A11 RID: 2577
	public HoverWheel[] hoverWheels;

	// Token: 0x04000A12 RID: 2578
	public WheelCheckGroup[] wheelGroups;

	// Token: 0x04000A13 RID: 2579
	private bool wheelLoopDone;

	// Token: 0x04000A14 RID: 2580
	public bool hover;

	// Token: 0x04000A15 RID: 2581
	[NonSerialized]
	public int groundedWheels;

	// Token: 0x04000A16 RID: 2582
	[NonSerialized]
	public Vector3 wheelNormalAverage;

	// Token: 0x04000A17 RID: 2583
	private Vector3 wheelContactsVelocity;

	// Token: 0x04000A18 RID: 2584
	[Tooltip("Lower center of mass by suspension height")]
	public bool suspensionCenterOfMass;

	// Token: 0x04000A19 RID: 2585
	public Vector3 centerOfMassOffset;

	// Token: 0x04000A1A RID: 2586
	[Tooltip("Tow vehicle to instantiate")]
	public GameObject towVehicle;

	// Token: 0x04000A1B RID: 2587
	private GameObject newTow;

	// Token: 0x04000A1C RID: 2588
	[NonSerialized]
	public VehicleParent inputInherit;

	// Token: 0x04000A1D RID: 2589
	[NonSerialized]
	public bool crashing;

	// Token: 0x04000A1E RID: 2590
	[Header("Crashing")]
	public bool canCrash = true;

	// Token: 0x04000A1F RID: 2591
	public AudioSource crashSnd;

	// Token: 0x04000A20 RID: 2592
	public AudioClip[] crashClips;

	// Token: 0x04000A21 RID: 2593
	[NonSerialized]
	public bool playCrashSounds = true;

	// Token: 0x04000A22 RID: 2594
	public ParticleSystem sparks;

	// Token: 0x04000A23 RID: 2595
	[NonSerialized]
	public bool playCrashSparks = true;

	// Token: 0x04000A24 RID: 2596
	[Header("Camera")]
	public float cameraDistanceChange;

	// Token: 0x04000A25 RID: 2597
	public float cameraHeightChange;
}
